function makeSpaces(number) {
	if (typeof number != "string")
		number = number.toString();
	var value = number.replace(/ /g,"");
	var count = Math.floor(value.length/3) - 1;
	var rest = value.length%3;
	if (rest != 0 && count > -1){
		value = value.substr(0,rest) + " " + value.substr(rest);
	}
	for (var i = 0; i < count; i++){
		var pos = (rest != 0 ? rest + 1 : 0) + (i*4) + 3;
		value = value.substr(0,pos) + " " + value.substr(pos);
	}
	return value;
}

function calc() {
	
	var total = 0;
	
	$(".section").each(function(){
		
		var elem = $(this).next();
		var section_sum = 0;
		while (elem.hasClass("calculate")) {
			var count = parseInt(elem.find(".calc_input_count")[0].val().replace(/ /g,""));
			var price = parseInt(elem.find(".calc_input_price")[0].val().replace(/ /g,""));
			count = isNaN(count) ? 0 : count;
			price = isNaN(price) ? 0 : price;
			var current_sum = count * price;
			section_sum += current_sum;
			total += current_sum;
			elem.find(".calc_input_sum")[0].html(makeSpaces(current_sum));
			elem = elem.next();
		}
		$(this).find(".section_summ")[0].html(makeSpaces(section_sum));
	})
	total = isNaN(total) ? "Ошибка" : makeSpaces(total);
	$("#calc_total").html(total);
}

$(document).ready(function(){
	calc();
	var pos = 0;
	$(".calc_input_name").live("keyup",function(){ $("#save_result").html(""); })
	$(".calc_input_count").live("keyup",function(){ calc(); $("#save_result").html(""); })
	$(".calc_input_price").live("keyup",function(e){
		pos = $(this).caret();
		if (e.keyCode < 96 || e.keyCode > 105)
			if (e.keyCode < 48 || e.keyCode > 57)
				if (e.keyCode != 8 && e.keyCode != 46)
					return false;
		var def = $(this).val();
		var chg = makeSpaces($(this).val());
		var dv = chg.length - def.length;
		$(this).val(chg).caret((dv > 0) ? (pos+dv) : (pos));
		calc();
		$("#save_result").html("");
	})
	
	// switch
	$(".section_switch_off").live("click",function(){
		var elem = $(this).parent().next();
		while (elem.hasClass("calculate")) {
			elem.fadeOut(0);
			elem = elem.next();
		}
		$(this).removeClass("section_switch_off").addClass("section_switch_on");
		$(this).find(".switcher")[0].html("+");
	})
	$(".section_switch_on").live("click",function(){
		var elem = $(this).parent().next();
		while (elem.hasClass("calculate")) {
			elem.fadeIn(0);
			elem = elem.next();
		}
		$(this).removeClass("section_switch_on").addClass("section_switch_off");
		$(this).find(".switcher")[0].html("–");
	})
	
	// save
	$('#calc_save').click(function(){
		var params = "";
		var i = 0;
		$(".section").each(function(){
			
			var elem = $(this).next();
			var section = $(this).attr('id');
			while (elem.hasClass("calculate")) {
				var name = elem.find(".calc_input_name")[0].val();
				var fixed = elem.find(".calc_input_name")[0].hasClass("fixed") ? "fixed" : "none";
				var count = parseInt(elem.find(".calc_input_count")[0].val().replace(/ /g,""));
				var price = parseInt(elem.find(".calc_input_price")[0].val().replace(/ /g,""));
				count = isNaN(count) ? 0 : count;
				price = isNaN(price) ? 0 : price;
				elem = elem.next();
				params += "objects%5B"+i+"%5D%5Bname%5D="+encodeURIComponent(name)+"&objects%5B"+i+"%5D%5Bcount%5D="+count+"&objects%5B"+i+"%5D%5Bprice%5D="+price+"&objects%5B"+i+"%5D%5Bsection%5D="+section+"&objects%5B"+(i++)+"%5D%5Bfixed%5D="+fixed+"&";
			}
		})
		$.ajax({
			url: '/modules/mod_wed_calc/ajax.php',
			type: "post",
			data: params,
			success: function(data){
				if (data == 1) {
					$("#save_result").html("<p class='saveSuccess'>Успешно сохранено</p>");
				} else {
					$("#save_result").html("<p class='saveFail'>Не удалось сохранить</p>");
				}
			},
			error: function(){
				$("#save_result").html("<p class='saveFail'>Не удалось сохранить</p>");
			}
		})
	});
})